<template>
  <div>
    <!-- banner -->
    <DetailsBanner v-if="detailData" :details="detailData"></DetailsBanner>

    <div class="detailsInfoList">
      <div class="infoList">
        <div class="infoTitle">
          {{ detailData && detailData.title }}
        </div>

        <div
          class="infoDetails"
          id="article"
          v-if="detailData"
          v-highlight
          v-html="detailData.info"
        ></div>

        <el-divider content-position="left">end</el-divider>
        <DetailsCopyright></DetailsCopyright>
        <!-- 评论 -->
        <DetailsComment
          v-if="detailData"
          :articleTitle="detailData.title"
        ></DetailsComment>
      </div>
      <div class="newInfo">
        <DetailsCatalog
          v-if="detailData && newsArtcile"
          :newsArtcile="newsArtcile"
          @updateDetail="updateDetail"
        >
          <u-anchor container="#article"></u-anchor>
        </DetailsCatalog>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineAsyncComponent } from 'vue'
import { useRoute } from 'vue-router'
import { getArticleDeatils } from '@/utils/http/articleHttp'
import { ElMessage } from 'element-plus'
// 文中代码块
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css' //样式
import type { Directive } from 'vue'
// 引入banner
const DetailsBanner = defineAsyncComponent(
  () => import('@/components/details/DetailsBanner.vue')
)
// 引入右边的
const DetailsCatalog = defineAsyncComponent(
  () => import('@/components/details/DetailsCatalog.vue')
)
// 引入下面版权声明
const DetailsCopyright = defineAsyncComponent(
  () => import('@/components/details/DetailsCopyright.vue')
)
// 引入评论组件
const DetailsComment = defineAsyncComponent(
  () => import('@/components/details/DetailsComment.vue')
)
// 文章数据
type detailData = {
  _id: string
  title: string
  info: string
  articleType: string
  bg: string
  createTime: string
}
const detailData = ref<detailData | null>()

// 最新的 两条数据

const newsArtcile = ref<detailData[]>()
// 获取数据
const route = useRoute()
const getDeatils = () => {
  if (!route.query.id) return
  getArticleDeatils(route.query.id as string).then((res) => {
    if (!res.data.info[0]) return ElMessage.error('没有这个文章 ')
    detailData.value = res.data.info[0]
    newsArtcile.value = res.data.articleNews.data
  })
}
getDeatils()

// 代码高亮

const vHighlight: Directive<HTMLElement> = (el) => {
  let blocks = el.querySelectorAll('pre code')
  blocks.forEach((block) => {
    hljs.highlightElement(block as HTMLElement)
  })
}
// 最新文章跳转事件
const updateDetail = (data: detailData) => {
  detailData.value = null
  setTimeout(() => {
    detailData.value = data
    window.scrollTo(0, 0)
  })
}
</script>

<style lang="scss" scoped>
.detailsInfoList {
  display: flex;
  justify-content: space-between;
  max-width: 1200px;
  margin: 10px auto;
  line-height: 1.8;
  .infoList {
    flex: 1;
    background: #fff;
    padding: 50px 40px;
    margin-right: 20px;
    overflow: auto;
    border-radius: 10px;
    .infoTitle {
      width: 100%;
      color: #333;
      padding: 7px 0;
      margin: 6px 0;
      font-size: 20px;
      font-weight: 700;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      border-bottom: 2px solid #ddd;
      user-select: text !important;
    }
    .infoDetails {
      user-select: text !important;
      letter-spacing: 1px;
      margin-bottom: 40px;
      :deep(img) {
        max-width: 100% !important;
        height: auto !important;
      }
    }
  }
}
.newInfo {
  width: 280px;
}

@media (max-width: 900px) {
  .newInfo {
    display: none;
  }
  .detailsInfoList {
    .infoList {
      padding: 20px;
      margin-right: 0;
    }
  }
}
</style>
